<?php 
/**
* admin 老师管理
*/
class Admin_teacher extends CI_Controller
{
	
	function __construct()
	{
		parent::__construct();
		//登陆状态检测
		if(!$this->session->userdata('user_in'))
			redirect('home');
		//权限检测
		if($this->session->userdata('role_id') != 1)
			redirect('home');
	}

	/**
	 * 教师管理的主界面
	 * @return [type]
	 */
	function index()
	{
		//准备数据
		$data = array();
		$data['title']='教师管理';
		$this->load->model('teacher_model');
		
		$data['teachers']=$this->teacher_model->get_all_teachers();
		
		$this->load->view('admin_teacher/index',$data);
	}     

	/**
	 * 添加一个老师
	 */
	function add()
	{
		//表单验证
		if($this->form_validation->run('admin_teacher/add'))
		{
			//post方法
			//读取数据
			$teacher_name=$this->input->post('teacher_name');
			$major_id=$this->input->post('major_id');
			$teacher_number=$this->input->post('teacher_number');


			//新建一个用户
			$user = array(
				'username' => $teacher_number,
				'password' => md5($teacher_number),
				'role_id' => 2,
				 );

			$this->load->model('user_model');
			$this->user_model->add($user);

			$user_id=$this->db->insert_id();

			$teacher = array(
				'user_id' => $user_id,
				'teacher_number' => $teacher_number,
				'name' => $teacher_name,
				'major_id' => $major_id
				 );

			$this->load->model('teacher_model');
			$this->teacher_model->add($teacher);

			redirect('admin_teacher/index');		
		}

		//get方法
		$data['title']='添加教师';
		$this->load->model('major_model');
		$data['majors']=$this->major_model->get_all();

		$this->load->view('admin_teacher/add',$data);
	}

	//查看老师名下的逻辑班
	function check_logic_class($teacher_id)
	{
		//准备数据
		$data = array();
		$data['title']='教师管理';

		$this->load->model('student_model');
		$this->load->model('major_model');
		$this->load->model('class_model');
		$this->load->model('logic_class_model');

		$this->load->model('teacher_model');
		$teacher=$this->teacher_model->get_by_id($teacher_id);
		
		$logic_classes=$this->logic_class_model->get_logic_classes_by_teacher_id($teacher['id']);
		$index=0;
		foreach ($logic_classes as $item) {
			$classes_list_by_logic_class[$index]=$this->class_model->get_classes_by_logic_class_id($item['id']);
			
			for ($i=0; $i < count($classes_list_by_logic_class[$index]); $i++) { 
				$classes_list_by_logic_class[$index][$i]['select_num']=$this->class_model->get_select_num_in_class($item['id'],$classes_list_by_logic_class[$index][$i]['id']);
				$classes_list_by_logic_class[$index][$i]['total_num']=$this->class_model->get_total_num_in_class($classes_list_by_logic_class[$index][$i]['id']);
			}
			
			$index++;	
		}

		$data['logic_classes']=$logic_classes;
		$data['teacher']=$teacher;
		$data['classes_list_by_logic_class']=$classes_list_by_logic_class;
		//var_dump($classes_list_by_logic_class);
		$this->load->view('admin_teacher/check_logic_class',$data);
	}

	/**
	 *查看逻辑班的考试模块
	 */
	function check_module_in_logic_class($logic_class_id,$teacher_id,$class_id=0)
	{
		//获取这个逻辑班的课程中的所有考试模块
		$this->load->model('logic_class_model');
		$this->load->model('module_model');
		$this->load->model('lecture_model');
		$this->load->model('teacher_model');
		$this->load->model('class_model');

		$teacher=$this->teacher_model->get_by_id($teacher_id);
		$logic_class = $this->logic_class_model->get_by_id($logic_class_id);
		//var_dump($logic_class);
		$module_in_logic_class=$this->module_model->get_module_in_logic_class($logic_class['lecture_id']);
		//var_dump($module_in_logic_class);
		$lecture_info = $this->lecture_model->get_lecture_by_id($logic_class['lecture_id']);
		$module_add_logic_class=array();
		foreach ($module_in_logic_class as $module_in_logic_class_item) 
		{
			//寻找每一个模块是否有可替换的定制模块
			$module_replace=$this->module_model->get_module_type_1_parent_teacher($module_in_logic_class_item['id'],$teacher['id']);
			if($module_replace)
				{
					foreach ($module_replace as $module_replace_item) 
					{
						$each_module_add_logic_class_replace = array(
								'module_id' => $module_replace_item['id'], 
								'name' => $module_replace_item['name'], 
								'time_limit' => $module_replace_item['time_limit'], 
								'type' => $module_replace_item['type'], 
								'parent_id' => $module_replace_item['parent_id'], 
								'teacher_id' => $module_replace_item['teacher_id'], 
								'big_lecture_id' => $module_replace_item['big_lecture_id'],
								'logic_class_id' => $logic_class
								['id'],
								'logic_class_number' => $logic_class
								['number'],
								'logic_class_teacher_id' => $logic_class
								['teacher_id'],
								'logic_class_type' => $logic_class
								['type'],
								'lecture_id' => $logic_class
								['lecture_id'],
								'lecture_name' => $lecture_info['name'],
								'big_lecture_name' => $lecture_info['big_lecture_name']
								);
						array_push($module_add_logic_class, $each_module_add_logic_class_replace);
					}
					
				}
				else
				{
					$each_module_add_logic_class = array(
								'module_id' => $module_in_logic_class_item['id'], 
								'name' => $module_in_logic_class_item['name'], 
								'time_limit' => $module_in_logic_class_item['time_limit'], 
								'type' => $module_in_logic_class_item['type'], 
								'parent_id' => $module_in_logic_class_item['parent_id'], 
								'teacher_id' => $module_in_logic_class_item['teacher_id'], 
								'big_lecture_id' => $module_in_logic_class_item['big_lecture_id'],
								'logic_class_id' => $logic_class
								['id'],
								'logic_class_number' => $logic_class
								['number'],
								'logic_class_teacher_id' => $logic_class
								['teacher_id'],
								'logic_class_type' => $logic_class
								['type'],
								'lecture_id' => $logic_class
								['lecture_id'],
								'lecture_name' => $lecture_info['name'],
								'big_lecture_name' => $lecture_info['big_lecture_name']
								);
					array_push($module_add_logic_class, $each_module_add_logic_class);
				}
		}
		//var_dump($module_add_logic_class);
		$data = array();
		$data['title']='考试知识点错误分析';
		$data['teacher']=$teacher;		
		$data['module_add_logic_class']=$module_add_logic_class;
		if($class_id)
		{
			$data['class']=$this->class_model->get_by_id($class_id);
			$this->load->view('admin_teacher/check_module_in_logic_class_student',$data);
		}
		
		else
		{
			$this->load->view('admin_teacher/check_module_in_logic_class',$data);
		}
	
	}

	//获取考试模块下的所有题目的相关的知识点，包括考点信息，返回以考点为单位分组的二维数组
	function get_all_knowledge_point_with_test_point_by_module($module_id)
	{
		$test_point_in_module = $this->test_point_model->get_by_module($module_id);
		if(!$test_point_in_module)
        return false;
		//var_dump($test_point_in_module);
		//获取考点下的所有知识点并组合到一个数组当中
		$all_knowledge_point = array();
		foreach ($test_point_in_module as $test_point_in_module_item) {
			$knowledge_point_of_test_point=$this->knowledge_point_model->get_knowledge_point_by_question_of_test_point($test_point_in_module_item['test_point_id']);
			//$knowledge_point_of_test_point['module_name']=$module['name'];
			foreach ($knowledge_point_of_test_point as $knowledge_point_of_test_point_item) {
				array_push($all_knowledge_point, $knowledge_point_of_test_point_item);
			}
			
		}
		return $all_knowledge_point;
	}

	//返回所有知识点分类的题目
	function get_questions_group_by_knowledge($all_knowledge_point)
	{
		$questions_group_by_knowledge = array();//存储按照知识点分类的题目的数组
		foreach ($all_knowledge_point as $all_knowledge_point_item) {
			$questions_of_test_point_and_knowledge_point = $this->question_model->get_by_test_point_and_knowledge_point($all_knowledge_point_item['test_point_id'],$all_knowledge_point_item['knowledge_point_id']);
			//$questions_of_test_point_and_knowledge_point['test_point_id']=$all_knowledge_point_item['test_point_id'];
			//$questions_of_test_point_and_knowledge_point['knowledge_point_id']=$all_knowledge_point_item['knowledge_point_id'];
			array_push($questions_group_by_knowledge, $questions_of_test_point_and_knowledge_point);

		}
		return $questions_group_by_knowledge;
	}

	/**
	 * 查看某个模块下，以班级为单位统计该课程下的所有班级的知识点错误情况
	 * @return [type] [description]
	 */
	function check_knowledge_points_in_module_of_class($teacher_id,$logic_class_id,$module_id)
	{
		$this->load->model('logic_class_model');
		$this->load->model('module_model');
		$this->load->model('teacher_model');
		$this->load->model('test_point_model');
		$this->load->model('knowledge_point_model');
		$this->load->model('question_model');
		$this->load->model('class_model');
		$this->load->model('student_model');
		$this->load->model('report_model');
		$logic_class=$this->logic_class_model->get_logic_class_by_id($logic_class_id);
		$teacher=$this->teacher_model->get_by_id($teacher_id);
		$module=$this->module_model->get_by_id($module_id);
		$all_knowledge_point = $this->get_all_knowledge_point_with_test_point_by_module($module_id);
		if(!$all_knowledge_point)
        return false;
    	//获取按照知识点分组的题目
		$questions_group_by_knowledge = $this->get_questions_group_by_knowledge($all_knowledge_point);
		if(!$questions_group_by_knowledge)
        return false;

		//查找逻辑班中的所有班级
		$classes_list_by_logic_class=$this->class_model->get_classes_by_logic_class_id($logic_class_id);
			
		for ($i=0; $i < count($classes_list_by_logic_class); $i++) 
		{ 
			$classes_list_by_logic_class[$i]['select_num']=$this->class_model->get_select_num_in_class($logic_class_id,$classes_list_by_logic_class[$i]['id']);
			$classes_list_by_logic_class[$i]['total_num']=$this->class_model->get_total_num_in_class($classes_list_by_logic_class[$i]['id']);
		}
		if(!$classes_list_by_logic_class)
        return false;
		//echo "班级列表";
		//var_dump($classes_list_by_logic_class);
		//获取每个班级中的所有学生
		$index=0;
		foreach ($classes_list_by_logic_class as $classes_list_by_logic_class_item) {
			$students=$this->student_model->get_students_by_logic_class_id_and_class_id($logic_class_id,$classes_list_by_logic_class_item['id']);
			$students_in_classes_list_by_logic_class[$index]=$students;
			//var_dump($students);
			++$index;
		}
		if(!$students_in_classes_list_by_logic_class)
        return false;

		//获取在某个模块考试中某学生的某道题是不是做错了
		$class_num=0;
		$class_knowledge_num=0;
		$wrong_num=0;
		$student_num_in_report = array();
		$student_num_in_report_count = 0;

		foreach ($students_in_classes_list_by_logic_class as $students_in_classes_list_by_logic_class_item) 
		{
			//拆分到每个班级
			//先计算每个班参加考试的学生的数量
			$student_num_in_report_of_class=0;

			foreach ($students_in_classes_list_by_logic_class_item as $students_in_classes_list_by_logic_class_item_item) 
			{
				//获取每个学生的考试分数
				$student_grade=$this->report_model->get_grade($students_in_classes_list_by_logic_class_item_item['id'],$module_id,$logic_class_id);
				if($student_grade)
					++$student_num_in_report_of_class;
			}			
			
			foreach ($questions_group_by_knowledge as $questions_group_by_knowledge_item) 
			{
				//拆分到每个知识点
				foreach ($questions_group_by_knowledge_item as $questions_group_by_knowledge_item_item) 
				{
					//拆分到每个题目
					foreach ($students_in_classes_list_by_logic_class_item as $students_in_classes_list_by_logic_class_item_item) 
					{
						//拆分到每个学生				
						$question_id=$questions_group_by_knowledge_item_item['id'];
						$student_id=$students_in_classes_list_by_logic_class_item_item['id'];
						$wrong_question=$this->report_model->get_student_wrong_question_by_module_logic_class($module_id,$logic_class_id,$student_id,$question_id);
						if($wrong_question)
							++$wrong_num;

					}
				}
				$wrong_num_of_class_knowledge[$class_num][$class_knowledge_num] = $wrong_num;
				++$class_knowledge_num;
				$wrong_num=0;
			}
			array_push($student_num_in_report, $student_num_in_report_of_class);
			$student_num_in_report_count=$student_num_in_report_count+$student_num_in_report_of_class;
			++$class_num;
			$class_knowledge_num=0;
		}
		//将参加考试的学生总数同样插入到数组尾部，便于在一列显示
		array_push($student_num_in_report,$student_num_in_report_count);
		//计算每个知识点错误数量的总和
		$wrong_num_of_knowledge_point = array();
		$wrong_percent_of_knowledge_point = array();
			
		for($i=0;$i<count($wrong_num_of_class_knowledge[0],0);++$i)
		{
			$wrong_num_of_each_knowledge_point=0;
			$wrong_percent_of_each_knowledge_point=0;
			foreach ($wrong_num_of_class_knowledge as $wrong_num_of_class_knowledge_item) 
			{
				$wrong_num_of_each_knowledge_point=$wrong_num_of_each_knowledge_point+$wrong_num_of_class_knowledge_item[$i];
			}
			
			if($student_num_in_report_count)
			{
				$wrong_percent_of_each_knowledge_point=round($wrong_num_of_each_knowledge_point/$student_num_in_report_count*100,2).'%';
			}
			else
			{
				$wrong_percent_of_each_knowledge_point=0;
			}
			array_push($wrong_num_of_knowledge_point, $wrong_num_of_each_knowledge_point);
			array_push($wrong_percent_of_knowledge_point, $wrong_percent_of_each_knowledge_point);
		}
		//将知识点错误数目和百分比数插入到学生的知识点错误数目数组中，以便导出到excel表格
		array_push($wrong_num_of_class_knowledge, $wrong_num_of_knowledge_point);
		array_push($wrong_num_of_class_knowledge, $wrong_percent_of_knowledge_point);
		//var_dump($wrong_num_of_class_knowledge);
		$this->handle_class_excel($all_knowledge_point,$classes_list_by_logic_class,$wrong_num_of_class_knowledge,$logic_class,$module,$student_num_in_report);
	}

//以学生为单位，查看班级内的学生知识点错误情况
	function check_knowledge_points_in_module_of_student_in_class($teacher_id,$logic_class_id,$module_id,$class_id)
	{
		$this->load->model('logic_class_model');
		$this->load->model('module_model');
		$this->load->model('teacher_model');
		$this->load->model('test_point_model');
		$this->load->model('knowledge_point_model');
		$this->load->model('question_model');
		$this->load->model('class_model');
		$this->load->model('student_model');
		$this->load->model('report_model');
		$class=$this->class_model->get_by_id($class_id);
		$logic_class=$this->logic_class_model->get_logic_class_by_id($logic_class_id);
		$teacher=$this->teacher_model->get_by_id($teacher_id);
		$module=$this->module_model->get_by_id($module_id);
		$all_knowledge_point = $this->get_all_knowledge_point_with_test_point_by_module($module_id);
		if(!$all_knowledge_point)
        return false;
    	//获取按照知识点分组的题目
		$questions_group_by_knowledge = $this->get_questions_group_by_knowledge($all_knowledge_point);
		if(!$questions_group_by_knowledge)
        return false;
    	$students=$this->student_model->get_students_by_logic_class_id_and_class_id($logic_class_id,$class_id);
    	//var_dump($students);
    	if(!$students)
        return false;

    	$student_num=0;
		$student_knowledge_num=0;
		$wrong_num=0;
		$student_in_report_num=0;
		$students_grade = array();
		
		foreach ($students as $students_item) 
		{
			//拆分到每个学生
			//获取每个学生的考试分数
			$student_grade=$this->report_model->get_grade($students_item['id'],$module_id,$logic_class_id);
			if($student_grade)
				++$student_in_report_num;
			array_push($students_grade, $student_grade);
			foreach ($questions_group_by_knowledge as $questions_group_by_knowledge_item) 
			{
				//拆分到每个知识点
				foreach ($questions_group_by_knowledge_item as $questions_group_by_knowledge_item_item) 
				{
					//拆分到每个题目
						//var_dump($questions_group_by_knowledge_item_item);
						//var_dump($students_in_classes_list_by_logic_class_item_item);
						$question_id=$questions_group_by_knowledge_item_item['id'];
						$student_id=$students_item['id'];
						$wrong_question=$this->report_model->get_student_wrong_question_by_module_logic_class($module_id,$logic_class_id,$student_id,$question_id);
						if($wrong_question)
							++$wrong_num;
				}
				$wrong_num_of_student_knowledge[$student_num][$student_knowledge_num] = $wrong_num;
				++$student_knowledge_num;
				$wrong_num=0;
			}
		++$student_num;
		$student_knowledge_num=0;
		}
		//将参加考试的学生人数的总数插入到最后，以便在excel表格中同一列显示
		array_push($students_grade, $student_in_report_num);
		//计算每个知识点的错误总数,并计算占参加考试的学生数目的百分比
		$wrong_num_of_knowledge_point = array();
		$wrong_percent_of_knowledge_point = array();
			
		for($i=0;$i<count($wrong_num_of_student_knowledge[0],0);++$i)
		{
			$wrong_num_of_each_knowledge_point=0;
			$wrong_percent_of_each_knowledge_point=0;
			foreach ($wrong_num_of_student_knowledge as $wrong_num_of_student_knowledge_item) 
			{
				$wrong_num_of_each_knowledge_point=$wrong_num_of_each_knowledge_point+$wrong_num_of_student_knowledge_item[$i];
			}
			if($student_in_report_num)
			{
				$wrong_percent_of_each_knowledge_point=round($wrong_num_of_each_knowledge_point/$student_in_report_num*100,2).'%';
			}
			else
			{
				$wrong_percent_of_each_knowledge_point=0;
			}
			
			array_push($wrong_num_of_knowledge_point, $wrong_num_of_each_knowledge_point);
			array_push($wrong_percent_of_knowledge_point, $wrong_percent_of_each_knowledge_point);
		}
		//将知识点错误数目和百分比数插入到学生的知识点错误数目数组中，以便导出到excel表格
		array_push($wrong_num_of_student_knowledge, $wrong_num_of_knowledge_point);
		array_push($wrong_num_of_student_knowledge, $wrong_percent_of_knowledge_point);
		$this->handle_student_excel($all_knowledge_point,$students,$wrong_num_of_student_knowledge,$logic_class,$module,$class,$students_grade);
	}

	//导出到表格
	function handle_class_excel($all_knowledge_point,$classes_list_by_logic_class,$wrong_num_of_class_knowledge,$logic_class,$module,$student_num_in_report)
	{
		// if(!$query)
        //return false;

	    // Starting the PHPExcel library
	    $this->load->library('PHPExcel');
	    $this->load->library('PHPExcel/IOFactory');

	    $objPHPExcel = new PHPExcel();
	    $objPHPExcel->getProperties()->setTitle("export")->setDescription("none");

	    $objPHPExcel->setActiveSheetIndex(0);

	    // Field names in the first row
	    //$fields = $query->list_fields();
	    //$col 列从0开始。$row 行从1开始
	    $col = 2;
	    $test_point_nr=0;
	    foreach ($all_knowledge_point as $all_knowledge_point_item)
	    {	if ($all_knowledge_point_item['test_point_id']!=$test_point_nr) 
	    	{
	    		$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, 1, '('.$all_knowledge_point_item['test_point_id'].')'.$all_knowledge_point_item['test_point_content']);
	    	}
	    	$test_point_nr = $all_knowledge_point_item['test_point_id'];
	    	
	    	$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, 2, '('.$all_knowledge_point_item['knowledge_point_id'].')'.$all_knowledge_point_item['knowledge_point_content']);    	
	        $col++;
	    }

	    $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0, 1, '考点');
	    $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0, 2, '知识点');
	    $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(1, 2, '参考人数');

	    $row = 3;

	    foreach ($student_num_in_report as $student_num_in_report_of_class)
	    {	
	    	$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(1, $row, $student_num_in_report_of_class);
	        $row++;
	    }

	    $row = 3;
	    $col = 0;
	    $count_name['name']="总和";
	    $percent_name['name']="百分比";
	    array_push($classes_list_by_logic_class, $count_name);
	    array_push($classes_list_by_logic_class, $percent_name);
	    foreach ($classes_list_by_logic_class as $classes_list_by_logic_class_item)
	    {	
	    	$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $classes_list_by_logic_class_item['name']);
	        $row++;
	    }
	    
	    // Fetching the table data
	    $row = 3;
	    foreach($wrong_num_of_class_knowledge as $wrong_num_of_class_knowledge_item)
	    {
	        $col = 2;
	        foreach ($wrong_num_of_class_knowledge_item as $wrong_num_of_class_knowledge_item_value)
	        {
	            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $wrong_num_of_class_knowledge_item_value);
	            $col++;
	        }

	        $row++;
	    }

	    $objPHPExcel->setActiveSheetIndex(0);

	    $objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5');

	    // Sending headers to force the user to download the file
	    header('Content-Type: application/vnd.ms-excel');
	    header('Content-Disposition: attachment;filename=""'.$logic_class['lecture_name']."(".$logic_class['number'].")班 ".$module['name']."考试知识点错误情况统计.xls");
	    header('Cache-Control: max-age=0');

	    $objWriter->save('php://output');
	}

		//导出到表格
	function handle_student_excel($all_knowledge_point,$students,$wrong_num_of_student_knowledge,$logic_class,$module,$class,$students_grade)
	{
		// if(!$query)
        //return false;

	    // Starting the PHPExcel library
	    $this->load->library('PHPExcel');
	    $this->load->library('PHPExcel/IOFactory');

	    $objPHPExcel = new PHPExcel();
	    $objPHPExcel->getProperties()->setTitle("export")->setDescription("none");

	    $objPHPExcel->setActiveSheetIndex(0);

	    // Field names in the first row
	    //$fields = $query->list_fields();
	    //$col 列从0开始。$row 行从1开始
	    $col = 3;
	    $test_point_nr=0;
	    foreach ($all_knowledge_point as $all_knowledge_point_item)
	    {	if ($all_knowledge_point_item['test_point_id']!=$test_point_nr) 
	    	{
	    		$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, 1,  '('.$all_knowledge_point_item['test_point_id'].')'.$all_knowledge_point_item['test_point_content']);
	    	}
	    	$test_point_nr = $all_knowledge_point_item['test_point_id'];
	    	
	    	$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, 2,  '('.$all_knowledge_point_item['knowledge_point_id'].')'.$all_knowledge_point_item['knowledge_point_content']);    	
	        $col++;
	    }

	    $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(1, 1, '考点');
	    $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(1, 2, '知识点');
	    $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(2, 2, '成绩');

	    $row = 3;
	    $count_name['student_id']=" ";
	    $percent_name['student_id']=" ";
	    $count_name['name']="总和";
	    $percent_name['name']="百分比";
	    array_push($students, $count_name);
	    array_push($students, $percent_name);
	    foreach ($students as $students_item)
	    {	
	    	$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0, $row, $students_item['student_id']);
	    	$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(1, $row, $students_item['name']);
	        $row++;
	    }

	    $row = 3;

	    foreach ($students_grade as $student_grade)
	    {	
	    	$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(2, $row, $student_grade);
	        $row++;
	    }
	    
	    // Fetching the table data
	    $row = 3;
	    foreach($wrong_num_of_student_knowledge as $wrong_num_of_student_knowledge_item)
	    {
	        $col = 3;
	        foreach ($wrong_num_of_student_knowledge_item as $wrong_num_of_student_knowledge_item_value)
	        {
	            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $wrong_num_of_student_knowledge_item_value);
	            $col++;
	        }

	        $row++;
	    }

	    $objPHPExcel->setActiveSheetIndex(0);

	    $objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5');

	    // Sending headers to force the user to download the file
	    header('Content-Type: application/vnd.ms-excel');
	    header('Content-Disposition: attachment;filename=""'.$logic_class['lecture_name']."(".$logic_class['number'].")班 ".$class['name']."班 ".$module['name']."考试知识点错误情况统计.xls");
	    header('Cache-Control: max-age=0');

	    $objWriter->save('php://output');
	}
	
	/**
	 * 异步jquery验证
	 * @return [type] [description]
	 */
	function check_teacher_number_exist()
	{
		$teacher_number=$this->input->post('teacher_number');
		$this->load->model('teacher_model');
		if($this->teacher_model->teacher_number_exist($teacher_number))
			echo json_encode(FALSE);
		else 
			echo json_encode(TRUE);
	}

	/**
	 * CI的表单回调函数验证
	 * @param  [type] $str [description]
	 * @return [type]      [description]
	 */
	function teacher_number_check($teacher_number)
	{
		$this->load->model('teacher_model');
		if($this->teacher_model->teacher_number_exist($teacher_number))
			return FALSE;
		else 
			return TRUE;
	}

	/**
	 * 删除一个老师
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	function delete($id)
	{
		$this->load->model('teacher_model');
		if(!$this->teacher_model->get_by_id($id))
			$this->load->view('error');
		if($this->teacher_model->delete_cascade($id))
			$this->session->set_flashdata('message','操作成功');
		else
			$this->session->set_flashdata('message','存在相关引用，操作失败');

		redirect('admin_teacher/index');

	}

	/**
	 * 编辑一个教师的信息
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	function edit($id=0)
	{
		if($id)
		{
			//get方法
			$this->load->model('teacher_model');
			$teacher=$this->teacher_model->get_by_id($id);
			if(!$teacher)
				return $this->load->view('error');
			$data['title']='编辑教师信息';
			$data['teacher']=$teacher;

			$this->load->model('major_model');
			$data['majors']=$this->major_model->get_all();

			$this->load->view('admin_teacher/edit',$data);
		}
		else
		{
			//post方法
			//读取数据
			$this->load->model('teacher_model');

			$id=$this->input->post('teacher_id');
			$teacher=$this->teacher_model->get_by_id($id);
			if(!$teacher)
				return $this->load->view('error');
			if($this->form_validation->run('admin_teacher/edit'))
			{
				$name=$this->input->post('teacher_name');
				$major_id=$this->input->post('major_id');
				//将模块写入数据库
				$item = array(
					'name' => $name,
					'major_id' => $major_id,
					 );
				
				//数据库操作
				if($this->teacher_model->update($id,$item))
				{
					$this->session->set_flashdata('message','操作成功');
					redirect('admin_teacher/index');	
				}
				else
				{
					$this->session->set_flashdata('message','操作失败');
					redirect('admin_teacher/index');	
				}
	
			}
			else
			{
				//验证失败
				//get
				$data['title']='编辑教师信息';
				$data['teacher']=$teacher;

				$this->load->model('major_model');
				$data['majors']=$this->major_model->get_all();

				$this->load->view('admin_teacher/edit',$data);
			}
				
		}
			
		

	}
}
 